<?php

/**
 *   NIT Online Judge (C)2012-2099
 *   Developer: ACM Team @ Ningbo Institute of Technology
 *   
 *   class_error.php 2012-10-27 14:40 dgsrz@vip.qq.com
 */

if (!defined('NOJ_ACCESS_PERMISSION')) {
	exit('Unauthorized Access');
}

class NOJ_Error {
	
	function runtime_error($msg, $show = true, $save = false, $halt = true) {
		if (empty($msg)) {
			$msg = 'Unknown error. Please contact administrator.';
		}
		$showtrace = NOJ_Error::trace_back();
		if ($save) {
			NOJ_Error::write_log($msg);
		}
		if ($show) {
			NOJ_Error::show_error($msg, 'sys', $showtrace);
		}
		exit();
	}
	
	function db_error($msg, $sql) {
		$showtrace = NOJ_Error::trace_back();
		
		$db = &NOJ_DB::instance();
		$dberrno = $db->errno();
		$dberror = $db->error();
		
		$showtrace .= '<br /><b>MySQL_Error:</b><br />';
		$showtrace .= "[No.$dberrno] $dberror<br />";
		NOJ_Error::show_error($msg, 'db', $showtrace);
		exit();
	}
	
	function judge_error($msg) {
		// present errors from judge
	}
	
	function trace_back() {
		$ignoreFunc = array(
			'NOJ_Error->trace_back',
			'NOJ_Error->runtime_error',
			'NOJ_Error->db_error',
			'NOJ_DBProvider->halt',
			'NOJ_DBProvider->query',
			'NOJ_DB::_exec');
		$present = '';
		$debug_backtrace = debug_backtrace();
		krsort($debug_backtrace, SORT_REGULAR);
		foreach ($debug_backtrace as $key => $value) {
			$path = str_replace(NOJ_ROOT, './', $value['file']);
			$func = isset($value['class']) ? $value['class'] : '';
			$func .= isset($value['type']) ? $value['type'] : '';
			$func .= isset($value['function']) ? $value['function'] : '';
			if (in_array($func, $ignoreFunc)) break;
			$present .= "[Line: $value[line]] ".$path." - $func<br />";
		}
		return $present;
	}
	
	function write_log($msg, $detail = '') {
		$message = NOJ_Error::clear($msg);
		$time = time();
		$file = NOJ_ROOT.'/data/log/'.date("Ym").'_error.log';
		$hash = md5($message);
		$core = &NOJ_Core::instance();
		$ip = $core->_get_client_ip();
		//to be continued
	}
	
	function show_error($msg, $level, $detail = '') {
		$host = $_SERVER['HTTP_HOST'];
		$title = $level == 'db' ? 'Database' : 'System';
		echo <<<EOT
<!DOCTYPE HTML>
<html>
<head>
	<title>$host - $title Error</title>
	<meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" />
	<style type="text/css">
	<!--
	body { background-color: white; color: black; }
	.bodytitle { font: bold 11pt/13pt verdana, arial, sans-serif; height: 30px; }
	.bodytext  { font: 9pt/11pt verdana, arial, sans-serif; }
	a:link     { color: #66ccff; } /* 66CCFF */
	a:visited  { color: #66ccff; }
	-->
	</style>
</head>
<body>
	<p class="bodytitle">NIT Online Judge has encountered a problem.</p>
	<p class="bodytext">
		<b>Debug_backtrace:</b><br />
		$detail
	</p>
	<p class="bodytext">
		Hints: $msg (<a href="http://acm.nit.net.cn/wiki/">Need help?</a>)
	</p>
</body>
</html>
EOT;
	}
	
	function clear($message) {
		return str_replace(array("\n", "\t", "\r"), " ", $message);
	}
	
}

?>